package cn.com.dhcc.app.pub.module.authmgr.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.base.BaseService;
import cn.com.dhcc.app.core.base.CmCommon;
import cn.com.dhcc.app.core.base.Dao;
import cn.com.dhcc.app.core.base.cache.CacheGroupName;
import cn.com.dhcc.app.core.base.log.LogType;
import cn.com.dhcc.app.core.component.db.SingleTransationCircleWithOutResult;
import cn.com.dhcc.app.core.component.db.orm.WhereCondition;
import cn.com.dhcc.app.core.component.log.Log;
import cn.com.dhcc.app.core.component.pager.Pager;
import cn.com.dhcc.app.core.component.pager.PagerData;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.para.CorePara.StaticPara;
import cn.com.dhcc.app.core.tag.CommonStaticCode.ENTITY_STATUS;
import cn.com.dhcc.app.core.tag.CommonStaticCode.YESNO;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.Table.SYS_BASE_TABLE;
import cn.com.dhcc.app.pub.module.authmgr.vo.RoleInfoInner;
import cn.com.dhcc.app.pub.module.resmgr.service.ResourceFilter;
import cn.com.dhcc.app.pub.module.resmgr.service.ResourceService;
import cn.com.dhcc.app.pub.module.resmgr.vo.LinkResource;
import cn.com.dhcc.app.pub.module.resmgr.vo.MenuResource;


/**
 * 权限管理服务
 * @日期：2013-6-6下午7:26:34
 * @作者：乔兵
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public class AuthService extends BaseService {
	//水利部上级单位默认编码
	public static final String ORG_PCODE = "000000000";
	@Autowired
	private ResourceService resourceService;
	
	public static final String CACHEED_RESOURCE_COLLECTION_KEY = "_RES_KEY_";

	Logger logger = Log.getLogger(LogType.SYSINNER);

	public Collection<MenuResource> getCurrentUserTopMenu(HttpSession httpSession,final boolean superAdmin) {
		@SuppressWarnings("unchecked")
		final List<MenuResource> userMenus = (List<MenuResource>) httpSession.getAttribute(StaticPara.USER_SESSION_MENUS_KEY);
		@SuppressWarnings("unchecked")
		final Map<String,MenuResource> publicUrlResourceList = (Map<String,MenuResource>) CmCommon.getCacheService().
				getFromCacheByKey(CacheGroupName.RESOURCE_CACHE,"获取公共资源列表", ResourceService.CACHEED_PUBLIC_RESOURCE_COLLECTION_KEY);
		ResourceFilter resourceFilter = new ResourceFilter() {
			@Override
			public boolean selectCurrentNode(MenuResource resource) {
				return ResourceService.isTopMenu(resource)&&
						((superAdmin?true:userMenus.contains(resource))||publicUrlResourceList.values().contains(resource));
			}
			@Override
			public boolean removeCurrentNode(MenuResource resource) {
				return false;
			}
			@Override
			public boolean openCurrentNode(MenuResource resource) {
				return false;
			}
			@Override
			public boolean nocheck(MenuResource resource) {
				return false;
			}
			@Override
			public boolean selectCurrentNode(LinkResource linkResource) {
				return false;
			}
			@Override
			public boolean removeCurrentNode(LinkResource linkResource) {
				return true;
			}
			@Override
			public boolean nocheck(LinkResource linkResource) {
				return false;
			}
		};
		Map<String,MenuResource> userTopMenu = resourceService.getResourceFromCache(ResourceService.getAllMenuResourceFromCache().values(),resourceFilter);
		return userTopMenu.values();
	}
	public Collection<MenuResource> getCurrentUserMenuNode(final String topMenuId, HttpSession httpSession,final boolean superAdmin) throws ServiceException {
		@SuppressWarnings("unchecked")
		final List<MenuResource> userMenus = (List<MenuResource>) httpSession.getAttribute(StaticPara.USER_SESSION_MENUS_KEY);
		@SuppressWarnings("unchecked")
		final Map<String,MenuResource> publicUrlResourceList = (Map<String,MenuResource>) CmCommon.getCacheService().
				getFromCacheByKey(CacheGroupName.RESOURCE_CACHE,"获取公共资源列表", ResourceService.CACHEED_PUBLIC_RESOURCE_COLLECTION_KEY);
		ResourceFilter resourceFilter = new ResourceFilter() {
			@Override
			public boolean selectCurrentNode(MenuResource resource) {
				return resource.getPid().equalsIgnoreCase(topMenuId)&&
						ResourceService.isLeftMenu(resource)&&
						((superAdmin?true:userMenus.contains(resource))||publicUrlResourceList.values().contains(resource));
			}
			@Override
			public boolean removeCurrentNode(MenuResource resource) {
				return false;
			}
			@Override
			public boolean openCurrentNode(MenuResource resource) {
				return false;
			}
			@Override
			public boolean nocheck(MenuResource resource) {
				return false;
			}
			@Override
			public boolean selectCurrentNode(LinkResource linkResource) {
				return false;
			}
			@Override
			public boolean removeCurrentNode(LinkResource linkResource) {
				return true;
			}
			@Override
			public boolean nocheck(LinkResource linkResource) {
				return false;
			}
		};
		MenuResource top = ResourceService.getMenuResourceFromCache(topMenuId);
		Map<String,MenuResource> userLeftMenu = resourceService.getResourceFromCache(top.getChildMenuResource(),resourceFilter);
		
		return userLeftMenu.values();
	}
	/*public static boolean hasAuth(String resourceId,String userId){
		@SuppressWarnings("unchecked")
		Map<String, List<String>> actionid_userIds = (Map<String, List<String>>) CmCommon.getCache().
				getAuthData("获取连接对应的用户列表资源", ResourceService.CACHEED_RESOURCEID_USERIDS_COLLECTION_KEY);
	    return hasAuth(actionid_userIds, resourceId, userId);
	}
	public static boolean hasAuth(Map<String, List<String>> resid_userIds,String resourceId,String userId){
	    return resid_userIds.get(resourceId.toLowerCase()).contains(userId);
	}*/
	/**
	 *  根据参数获取角色分页列表
	 * @param parameter 
	 * @param offset
	 * @param pagerSize
	 * @return {List<HowTo>,total}
	 * @throws NiceException
	 */
	public PagerData<RoleInfoInner> listRole(Pager pager, WhereCondition parameter) throws ServiceException {
		try {
			return CmCommon.getDao().getPagerModuleList("获取角色列表",RoleInfoInner.class,parameter,pager);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	/**
	 * 根据参数获取角色列表
	 * @param parameter 
	 * @param offset
	 * @param pagerSize
	 * @return {List<HowTo>,total}
	 * @throws NiceException
	 */
	public List<RoleInfoInner> listRole(WhereCondition parameter) throws ServiceException {
		try {
			return CmCommon.getDao().listModule("获取角色列表", RoleInfoInner.class,parameter);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	/**
	 * 
	 * @param offset
	 * @param pagerSize
	 * @return {List<HowTo>,total}
	 * @throws NiceException
	 */
	public List<RoleInfoInner> listUserRole(WhereCondition parameter) throws ServiceException {
		String preWhereSql = "SELECT t_role.id,role_name,role_status,isdefault," +
				" decode(t_user_role.role_id,null,'false','true') checked" +
				" FROM  "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_ROLE)+"  t_role  " +
				" LEFT JOIN "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_USER_ROLE)+" t_user_role"+
                " ON t_role.id=t_user_role.role_id ";
		try {
			return CmCommon.getDao().listModule("根据用户获取角色列表", preWhereSql, RoleInfoInner.class,parameter);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	/**
	 * 
	 * @param offset
	 * @param pagerSize
	 * @return {List<HowTo>,total}
	 * @throws NiceException
	 */
	public List<RoleInfoInner> listJObRole(WhereCondition parameter) throws ServiceException {
		String preWhereSql = "SELECT t_role.*," +
				" decode(t_job_role.role_id,null,'false','true') checked" +
				" FROM  "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_ROLE)+"  t_role  " +
				" LEFT JOIN "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_JOB_ROLE)+" t_job_role"+
                " ON t_role.id=t_job_role.role_id ";
		try {
			return CmCommon.getDao().listModule("根据岗位获取角色列表", preWhereSql, RoleInfoInner.class,parameter);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	/**
	 * 
	 * @param offset
	 * @param pagerSize
	 * @return {List<HowTo>,total}
	 * @throws NiceException
	 */
	public List<RoleInfoInner> listRoleFromJob(String[] jobs) throws ServiceException {
		String preWhereSql = "SELECT t_role.* " +
				" FROM  "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_ROLE)+"  t_role,  " +
				 WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_JOB_ROLE)+" t_job_role"+
                " where  t_role.id=t_job_role.role_id ";
		WhereCondition whereCondition = new WhereCondition();
		whereCondition.andIn("job_code", jobs);
		try {
			return CmCommon.getDao().listModule("根据岗位获取角色列表", preWhereSql, RoleInfoInner.class,whereCondition);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	public boolean roleNameCheck(String newValue, String oldValue) throws ServiceException {
		if(newValue==null||newValue.trim().equalsIgnoreCase(oldValue)){
			return false;
		}
		else {
			newValue = newValue.toUpperCase();
			String sql = "SELECT count(id) FROM "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_ROLE)+" WHERE role_name=?";
			try {
				int num = CmCommon.getDao().queryForInt("验证角色名"+newValue+"是否存在", sql, new Object[]{newValue});
				return (num>0)?true:false;
			} catch (DaoException e) {
				throw new ServiceException(e);
			}
		}
		
	}
	public boolean roleCodeCheck(String newValue, String oldValue) throws ServiceException {
		if(newValue==null||newValue.trim().equalsIgnoreCase(oldValue)){
			return false;
		}
		else {
			newValue = newValue.toUpperCase();
			String sql = "SELECT count(id) FROM "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_ROLE)+" WHERE role_code=?";
			try {
				int num = CmCommon.getDao().queryForInt("验证角色名"+newValue+"是否存在", sql, new Object[]{newValue});
				return (num>0)?true:false;
			} catch (DaoException e) {
				throw new ServiceException(e);
			}
		}
		
	}
	/**
	 * 水资源添加角色
	 * @param roleInfo
	 * @param rsids
	 * @throws ServiceException
	 */
	public void addRoleSZY(final RoleInfoInner roleInfo,String[] rsids) throws ServiceException {
		if(roleInfo==null)return;
		final Date executeDate = new Date();
		String muid_c = BaseController.getUid();
		roleInfo.setModified(executeDate);
		roleInfo.setMuid(muid_c);
		roleInfo.setId(UUIDGenerator.getUUID());
		roleInfo.setRoleStatus(ENTITY_STATUS.ENABLE.getCode());
		roleInfo.setIsDefault(YESNO.NO.getStatus());
		String roleCode = "R_"+roleInfo.getId();
		roleInfo.setRoleCode(roleCode);
		final String addrole_right_sql = "INSERT INTO "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_ROLE_RIGHT)+" (role_id,right_id) VALUES(?,?)";
		final List<Object[]> args = new ArrayList<Object[]>();
		
		if(StrUtil.isAllNotBlank(rsids)){
			if(rsids.length!=0){
				Object[] arg = null;
				for (int i = 0; i < rsids.length; i++) {
					arg = new Object[2];
					arg[0] = roleInfo.getId();
					arg[1] = rsids[i];
					args.add(arg);
				}
			}
		}
		final Dao dao = CmCommon.getDao();
		try {
			dao.doInSingleTransationCircle("添加角色", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						dao.insertModule("添加角色", roleInfo);
						if(!args.isEmpty()){
						 dao.batchInsert("添加角色权限关系", addrole_right_sql,args);
						}
					} catch (DaoException e) {
						throw new RuntimeException(e);
					}
				}
			});
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	/**
	 * 水利同一用户添加角色，需要和单位挂钩生成角色
	 * @param roleInfoArgs
	 * @param rsids
	 * @param orgCodes
	 * @param orgNames
	 * @throws ServiceException
	 * @throws NiceException
	 */
	public void addRoleSL(final RoleInfoInner roleInfoArgs,String[] rightIds,String[] orgCodes,String[] orgNames) throws ServiceException, NiceException {
		if(roleInfoArgs==null)return;
		if(!StrUtil.isNotBank(orgCodes)){
			throw new NiceException("请选择部门!");
		}
		final Date executeDate = new Date();
		String muid_c = BaseController.getUid();
		//根据部门生成角色
		final StringBuffer existsRoleMsg = new StringBuffer();
		final List<RoleInfoInner> orgRoles = new ArrayList<RoleInfoInner>();
		for (int i = 0; i < orgCodes.length; i++) {
			String orgCode = orgCodes[i];
			String orgName = orgNames[i];
			String roleName = orgName+"_"+roleInfoArgs.getRoleName();
			//验证角色名称是否重复，重复提示！
			boolean exists = roleNameCheck(roleName, null);
			if(exists){
				existsRoleMsg.append(roleName);
				if(i!=orgCodes.length-1){
					existsRoleMsg.append(",");
				}else{
					existsRoleMsg.insert(0, "添加成功，但以下角色重复：").append("；");
				}
			}else{
				RoleInfoInner orgRole = new RoleInfoInner();
				try {
					org.springframework.beans.BeanUtils.copyProperties(roleInfoArgs,orgRole);
				} catch (Exception e) {
					throw new ServiceException(e);
				}
				orgRole.setOrgCode(orgCode);
				orgRole.setId(UUIDGenerator.getUUID());
				String roleCode = orgCode+"_"+orgRole.getId();
				orgRole.setRoleCode(roleCode);
				orgRole.setRoleStatus(ENTITY_STATUS.ENABLE.getCode());
				orgRole.setRoleName(roleName);
				orgRole.setIsDefault(YESNO.NO.getStatus());
				orgRole.setModified(executeDate);
				orgRole.setMuid(muid_c);
				orgRoles.add(orgRole);
			}
			
		}
		////根据部门生成角色 end
		final String addrole_right_sql = "INSERT INTO "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_ROLE_RIGHT)+" (role_id,right_id) VALUES(?,?)";
		
		final List<Object[]> resorceRoleArgs = new ArrayList<Object[]>();
		//生成批量插入参数
		
		final String desc = "添加角色"; 
		if(StrUtil.isAllNotBlank(rightIds)){
			if(rightIds.length!=0){
				
				for (Iterator<RoleInfoInner> iterator = orgRoles.iterator(); iterator
						.hasNext();) {
					RoleInfoInner roleInfo = iterator.next();
					for (int i = 0; i < rightIds.length; i++) {
						Object[] argrr = null;
						argrr = new Object[2];
						argrr[0] = roleInfo.getId();
						argrr[1] = rightIds[i];
						resorceRoleArgs.add(argrr);
					}
				}
			}
		}
		
		final Dao dao = CmCommon.getDao();
		try {
			dao.doInSingleTransationCircle(desc, new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						if(!orgRoles.isEmpty()){
							dao.batchInsertModule("批量插入角色", orgRoles);
						}
						if(!resorceRoleArgs.isEmpty()){
						  dao.batchInsert(desc, addrole_right_sql,resorceRoleArgs);
						}
					} catch (DaoException e) {
						throw new RuntimeException(e);
					}
				}
			});
			if(StrUtil.isNotBank(existsRoleMsg.toString())){
				throw new NiceException(existsRoleMsg.toString());
			}
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	/**
	 * 
	 * @param roleInfo
	 * @param resourceStr
	 * @throws ServiceException
	 */
	public void updateRole(final RoleInfoInner roleInfo, String[] rightIds) throws ServiceException{
		if(roleInfo==null)return;
		final Date executeDate = new Date();
		String muid_c =  BaseController.getUid();
		roleInfo.setModified(executeDate);
		roleInfo.setMuid(muid_c);
		
		final String deleteRole_right_sql = "DELETE FROM "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_ROLE_RIGHT)+" WHERE role_id=?";
		final String addRole_right_sql = "INSERT INTO "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_ROLE_RIGHT)+" (role_id,right_id) VALUES(?,?)";
		final List<Object[]> args = new ArrayList<Object[]>();
		
		if(StrUtil.isAllNotBlank(rightIds)){
			if(rightIds.length!=0){
				Object[] arg = null;
				for (int i = 0; i < rightIds.length; i++) {
					arg = new Object[2];
					arg[0] = roleInfo.getId();
					arg[1] = rightIds[i];
					args.add(arg);
				}
			}
		}
		
		final Dao dao = CmCommon.getDao();
		
		try {
			dao.doInSingleTransationCircle("修改角色", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						dao.updateModuleById("修改角色", roleInfo, null);
						dao.delete("删除角色资源关联", deleteRole_right_sql, new Object[]{roleInfo.getId()});
						if(!args.isEmpty()){
							dao.batchInsert("修改角色资源关联", addRole_right_sql,args);
						}
					} catch (DaoException e) {
						throw new RuntimeException(e);
					}
				}
			});
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
		
	}
	
	
	
	public RoleInfoInner getRole(String roleId) throws ServiceException {
		try {
			RoleInfoInner roleInfo = getDao().getModuleById("根据角色id+["+roleId+"]获取角色", RoleInfoInner.class, new Object[]{roleId});
			return roleInfo;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	public void changeRoleStatus(final String roleId,final ENTITY_STATUS toRoleStatus) throws ServiceException, NiceException {
		final RoleInfoInner roleInfo = getRole(roleId);
		if(roleInfo==null){
			throw new NiceException("操作失败，角色已不存在");
		}
		if(toRoleStatus.getCode().equalsIgnoreCase(roleInfo.getRoleStatus())){
			throw new NiceException("操作失败，角色已为"+toRoleStatus.getName()+"状态");
		}
		final String sql = "UPDATE "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_ROLE)+" SET role_status=? WHERE id=?";
		
		final Dao dao = CmCommon.getDao();
		try {
			dao.doInSingleTransationCircle("删除角色["+roleInfo.getRoleName()+"]", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						if(toRoleStatus==ENTITY_STATUS.REL_DELETE){
							String del_role_res_rel_sql = "delete from "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_ROLE_RIGHT)+" WHERE ROLE_ID=?";
							 dao.delete("删除资源角色关系，角色id["+roleInfo.getId()+"]角色", del_role_res_rel_sql,new Object[]{roleId});
						}
						dao.update("修改id["+roleInfo.getRoleName()+"]角色为："+toRoleStatus.getName(), sql,new Object[]{toRoleStatus.getCode(),roleId});
					} catch (DaoException e) {
						throw new RuntimeException(e);
					}
				}
			});
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	public void deleteRole(final String roleId) throws ServiceException, NiceException {
		final RoleInfoInner roleInfo = getRole(roleId);
		if(roleInfo==null){
			throw new NiceException("操作失败，角色已不存在");
		}
		final String del_role_sql = "DELETE FROM  "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_ROLE)+" WHERE id=?";
		final String del_role_resource_sql = "DELETE FROM "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_ROLE_RIGHT)+" WHERE role_id=?";
		final Dao dao = CmCommon.getDao();
		try {
			dao.doInSingleTransationCircle("删除角色["+roleInfo.getRoleName()+"]", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						 dao.delete("删除角色权限关联", del_role_resource_sql, new Object[]{roleId});
						 dao.delete("删除id["+roleInfo.getRoleName()+"]角色", del_role_sql,new Object[]{roleId});
					} catch (DaoException e) {
						throw new RuntimeException(e);
					}
				}
			});
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	public void assignUserRole(final String userId, String[] roleId) throws ServiceException {
		//先删除
		final String delete_current_user_roles_sql = "DELETE FROM "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_USER_ROLE)+" WHERE user_id=?";
		final String insert_current_user_roles_sql = "INSERT INTO "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_REL_USER_ROLE)+" (user_id,role_id) VALUES(?,?)";
		
		final List<Object[]> args = new ArrayList<Object[]>();
		if(roleId!=null){
			for (int i = 0; i < roleId.length; i++) {
				args.add(new Object[]{userId,roleId[i]});
			}
		}
		final Dao dao = CmCommon.getDao();
		try {
			dao.doInSingleTransationCircle("给用户["+userId+"]添加或修改角色", new SingleTransationCircleWithOutResult() {
				@Override
				public void actionInCircle() throws RuntimeException {
					try {
						 dao.delete("删除用户["+userId+"]所含角色", delete_current_user_roles_sql,new Object[]{userId});
						 if(args.size()!=0){
						  dao.batchInsert("批量插入用户角色关系", insert_current_user_roles_sql, args);
						 }
					} catch (DaoException e) {
						throw new RuntimeException(e);
					}
				}
			});
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	public void assignJobRole(final String jobCode, String[] rids) throws ServiceException {
		//先删除
				final String delete_current_user_roles_sql = "DELETE FROM "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_JOB_ROLE)+" WHERE job_code=?";
				final String insert_current_user_roles_sql = "INSERT INTO "+WSD_TABLE(SYS_BASE_TABLE.SYS_BASE_JOB_ROLE)+" (job_code,role_id) VALUES(?,?)";
				
				final List<Object[]> args = new ArrayList<Object[]>();
				if(rids!=null){
					for (int i = 0; i < rids.length; i++) {
						args.add(new Object[]{jobCode,rids[i]});
					}
				}
				final Dao dao = CmCommon.getDao();
				try {
					dao.doInSingleTransationCircle("给岗位["+jobCode+"]添加或修改角色", new SingleTransationCircleWithOutResult() {
						@Override
						public void actionInCircle() throws RuntimeException {
							try {
								 dao.delete("删除岗位["+jobCode+"]所含角色", delete_current_user_roles_sql,new Object[]{jobCode});
								 if(args.size()!=0){
								  dao.batchInsert("批量插入岗位角色关系", insert_current_user_roles_sql, args);
								 }
							} catch (DaoException e) {
								throw new RuntimeException(e);
							}
						}
					});
				} catch (DaoException e) {
					throw new ServiceException(e);
				}
		
	}
	public void pushRole() {
		
	}
	
	/*public UamsService getUamsService(){
		//更新本地用户信息
		String wsdl = UamsPluginPara.UAMS_WSDL;
		return (UamsService) WebserviceUtil.getServicePort(wsdl,UamsService.class);
	}*/
	/**
	 * 获取所有的组织机构信息节点
	 * @param searchMap 
	 * @return
	 * @throws ServiceException
	 */
	public List<Map<String, Object>> getOrgInfoNodeList(String iconPath, Map<String, String> searchMap, String  adminUserCode,HttpSession httpSession,String hasSelectedOrgCodes) throws ServiceException {
		/*UamsService service = getUamsService();
		//加载全部的输信息
		List<Map<String, Object>> nodeList = new ArrayList<Map<String, Object>>();
		List<cn.com.dhcc.app.core.api.uams.sl.OrgInfo> orgInfoList = service.getOrgInfoList();
		List<String> selectedOrgCodelist = new ArrayList<String>();
		if(StrUtil.isNotBank(hasSelectedOrgCodes)){
			selectedOrgCodelist = Arrays.asList(hasSelectedOrgCodes.split(","));
		}
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("id", "0");
		root.put("nocheck", true);
		root.put("name", "组织机构信息");
		root.put("pId", -1);
		SsoUser superSsoUser = null;//CmUserMgrService.getSuperAdmin();
		if(adminUserCode.equalsIgnoreCase(superSsoUser.getUserCode())){
			root.put("admin", superSsoUser.getUserName());
		}
		root.put("open", true);
		root.put("icon", iconPath);
		nodeList.add(root);
		if (CollectionUtil.hasElement(orgInfoList)) {
			for (cn.com.dhcc.app.core.api.uams.sl.OrgInfo orgInfo : orgInfoList) {
				Map<String, Object> node = new HashMap<String, Object>();
				node.put("id", orgInfo.getOrgCode());
				String tmpPId  = StrUtil.isNotBlank(orgInfo.getPcode()) ? orgInfo.getPcode() : "0";
				if(selectedOrgCodelist.contains(orgInfo.getOrgCode())){
					node.put("checked", true);
					node.put("open", true);
				}
				if(tmpPId.equals(ORG_PCODE))
				{
					node.put("open", true);
					node.put("pId", "0");
				}else
				{
					node.put("pId", tmpPId);
				}
				node.put("name", orgInfo.getOrgName());

				node.put("orgCode", orgInfo.getOrgCode());
				node.put("pcode", StrUtil.isNotBlank(orgInfo.getPcode()) ? orgInfo.getPcode() : "0");
				node.put("ifdel", orgInfo.getStatus());
				if(ENTITY_STATUS.DISABLE.getCode().equals(orgInfo.getStatus())){
					node.put("iconSkin", "iconLock");
				}
				if(searchMap.containsKey(orgInfo.getOrgCode())){
					node.put("open", true);
					node.put("searchHit", true);
				}
				nodeList.add(node);
			}
		}
		return nodeList;*/
		return null;
	}
}

